<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\tool\Result;
use think\Response;
use think\response\Json;

class Spec extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Spec';

    protected array $append = ['specText'];

    //自定义方法排序
    protected array $sort = ['id' => 'desc'];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['fixed' => 'left', 'title' => '全选', 'type' => 'checkbox'])
            ->addColumn(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '规格名称'])
            ->addColumn(['field' => 'spec', 'title' => '状态', 'templet' => '#specTpl'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 150])
            ->addRightButtons(['edit', 'delete'])
            ->addTopButtons(['add', 'deleteAll'])
            ->addSearch([
                ['title', '规格名称', 'input,Like'],
            ])
            ->addLineStyle('height: 65px;')
            ->addExtraHtml(['spec'])
            ->fetch();
    }

    public function add($tpl = 'spec/edit'): string
    {
        return parent::add($tpl);
    }

    public function edit($id = '', $tpl = 'spec/edit', $info = ''): string
    {
        return parent::edit($id, $tpl, $info);
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model);
    }

    public function getSpec($id = '', $type = 1): Response
    {
        if ($type == 1) {
            $list = $this->model::where('id', $id)->append(['specText'])->find();
        } else {
            $list = $this->model::field('id,title')->order('create_time', 'desc')->select();
        }

        return Result::success('success', $list);
    }
}